#region LicenseHeader

// Copyright 2012 The Trustees of Indiana University.  All rights reserved.
// 
// The Indiana University licenses this file to you under the Apache License, 
// Version 2.0 (the "License"); you may not use this file except in compliance 
// with the License.  You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// @author Thilina Gunarathne (tgunarat@indiana.edu)

#endregion

#region

using System;
using System.IO;
using System.Reflection;
using NAligner.Utils;

#endregion

namespace NAligner
{
    [Serializable]
    public class ScoringMatrix
    {
        private const char COMMENT_STARTER = '#';
        private const int SIZE = 127;

        private readonly string _matrixName;
        private readonly float[,] _scores;

        public ScoringMatrix(string matrixName, float[,] scores)
        {
            _matrixName = matrixName;
            _scores = scores;
        }

        public string Name
        {
            get { return _matrixName; }
        }

        public float[,] Scores
        {
            get { return _scores; }
        }

        public float GetScore(char a, char b)
        {
            return _scores[a, b];
        }

        public static ScoringMatrix Load(string matrixName)
        {
            char[] acids = new char[SIZE];

            // Initialize the acids array to null values (ascii = 0)
            for (int i = 0; i < SIZE; i++)
            {
                acids[i] = (char) (0);
            }

            float[,] scores = new float[SIZE,SIZE];

            using (
                Stream stream =
                    Assembly.GetExecutingAssembly().GetManifestResourceStream("NAligner.matrices." + matrixName))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    // Skip the comment lines
                    string line;

                    while ((line = reader.ReadLine()) != null && line.Trim()[0] == COMMENT_STARTER)
                        ;

                    // Read the headers line (the letters of the acids)
                    Tokenizer tokenizer = new Tokenizer(line.Trim());

                    for (int j = 0; tokenizer.HasMoreTokens(); j++)
                    {
                        acids[j] = tokenizer.NextToken()[0];
                    }

                    // Read the scores
                    while ((line = reader.ReadLine()) != null)
                    {
                        tokenizer = new Tokenizer(line.Trim());
                        char acid = tokenizer.NextToken()[0];

                        for (int i = 0; i < SIZE; i++)
                        {
                            if (acids[i] != 0)
                            {
                                scores[acid, acids[i]] = Single.Parse(tokenizer.NextToken());
                            }
                        }
                    }
                }
            }

            return new ScoringMatrix(matrixName, scores);
        }
    }
}